/**
 * 
 */
package sk.stuba.fiit.foo07.genex.gui;

import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;

import sk.stuba.fiit.foo07.genex.beans.Category;
import sk.stuba.fiit.foo07.genex.dao.CategoryDao;
import sk.stuba.fiit.foo07.genex.dao.CategoryDaoDerby;

/**
 * @author _mizu_
 * 
 */
public class QuestionCategoryTreeListener implements TreeWillExpandListener,
        TreeSelectionListener {

    CategoryDao catDao;
    GenexTableModel genex;

    JTable genexTable;

    public QuestionCategoryTreeListener(Connection c, GenexTableModel model) {
        catDao = new CategoryDaoDerby(c);
        this.genex = model;
    }

    public QuestionCategoryTreeListener(Connection c) {
        catDao = new CategoryDaoDerby(c);
    }

    @Override
    public void treeWillCollapse(TreeExpansionEvent event)
            throws ExpandVetoException {
        TreePath path = event.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
                .getLastPathComponent();
        node.removeAllChildren();
        node.add(new DefaultMutableTreeNode("camuflage"));
    }

    @Override
    public void treeWillExpand(TreeExpansionEvent event)
            throws ExpandVetoException {
        TreePath path = event.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
                .getLastPathComponent();
        node.removeAllChildren();

        Category c = (Category) node.getUserObject();
        //TODO 
        //pridane odchytavanie vynimiek
        try {
            ArrayList<Category> subCats = catDao.getCategoriesByParentID(c
                    .getCategoryID());
            for (Category cat : subCats) {
                DefaultMutableTreeNode toAdd = new DefaultMutableTreeNode(cat);
                node.add(toAdd);

                if (catDao.hasSubcategories(cat.getCategoryID()))
                    toAdd.add(new DefaultMutableTreeNode("camuflage"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void valueChanged(TreeSelectionEvent e) {
        TreePath path = e.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
                .getLastPathComponent();
        Category c = (Category) node.getUserObject();
        genex.setActualCategory(c);

        if (genexTable != null) {
            //String spath = path.toString().replaceAll(", ", "-");
            genexTable.getColumnModel().getColumn(1).setHeaderValue(path);
            genexTable.getTableHeader().repaint();
        }
    }

    public void setGenexTable(JTable genexTable) {
        this.genexTable = genexTable;
    }
}
